package fx2048;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient {

    private String intAddress;


    private int port;
    private Socket socket;

    public SocketClient(String intAddress, int port) {
        this.intAddress = intAddress;
        this.port = port;
        try {
            this.socket = new Socket(intAddress, port);
            System.out.println("客户端启动成功");

        } catch (java.net.ConnectException e) {
            System.out.println("客户端启动失败");
            System.out.println("可能原因：");
            System.out.println("         1.未开启加速球");
            System.out.println("         2.加速球关闭自动切换游戏模式");
        } catch (IOException e) {
            if (this.socket != null) {
                try {
                    this.socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
        }
    }

    public String send(String order) {

        if (!socketAvailable()) {
            return "sendFail";
        }else {
            // 由系统标准输入设备构造BufferedReader对象
            try (PrintWriter write = new PrintWriter(socket.getOutputStream())) {
                // 由Socket对象得到输出流，并构造PrintWriter对象

                // 由Socket对象得到输入流，并构造相应的BufferedReader对象

                write.println(order);
                write.flush();

                //4、关闭资源
                write.close(); // 关闭Socket输出流

                socket.close(); // 关闭Socket

                System.out.println("发送命令成功");

                return "sendSuccess";
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("发送命令失败");
                return "sendFail";
            }
            //          in.close(); // 关闭Socket输入流
        }

    }


    // 搭建客户端
    public static void main(String[] args) throws IOException {
        try {
            // 1、创建客户端Socket，指定服务器地址和端口
            // Socket socket=new Socket("127.0.0.1",5200);
            Socket socket = new Socket("127.0.0.1", 5209);
            System.out.println("客户端启动成功");

            // 2、获取输出流，向服务器端发送信息
            // 向本机的52000端口发出客户请求

            // 由系统标准输入设备构造BufferedReader对象
            PrintWriter write = new PrintWriter(socket.getOutputStream());
            // 由Socket对象得到输出流，并构造PrintWriter对象
            //3、获取输入流，并读取服务器端的响应信息
            //        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 由Socket对象得到输入流，并构造相应的BufferedReader对象
            String readline;
            readline = "start";
            write.println(readline);
            write.flush();

            //4、关闭资源
            write.close(); // 关闭Socket输出流
            //          in.close(); // 关闭Socket输入流
            socket.close(); // 关闭Socket
        } catch (Exception e) {
            System.out.println("can not listen to:" + e);// 出错，打印出错信息
        }
    }

    public boolean socketAvailable() {
        if (this.socket != null) {
            System.out.println("建立连接成功，状态：可用");
            return true;
        }
        System.out.println("建立连接失败，状态：不可用");
        return false;
    }

}
